﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PICGEN
{
    public partial class Form1 : Form
    {
        string path = "";
        string currentpic = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            label1.Text = folderBrowserDialog1.SelectedPath;
            path = folderBrowserDialog1.SelectedPath;
            DirectoryInfo root = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
            FileInfo[] pics = root.GetFiles("*.png");

            listOld.Items.Clear();
            for (int i = 0; i < pics.Length; i++)
            {
                listOld.Items.Add(pics[i].Name);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //翻转
            Bitmap Img = new Bitmap(folderBrowserDialog1.SelectedPath + "//" + currentpic);
            Img.RotateFlip(RotateFlipType.Rotate180FlipY);
            pictureBox1.Image = Img;
            this.Refresh();
        }

        private void listOld_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentpic = listOld.SelectedItem.ToString();
            Bitmap Img = new Bitmap(folderBrowserDialog1.SelectedPath + "//" + currentpic);
            // Img.RotateFlip(RotateFlipType.Rotate180FlipY);
            pictureBox1.Image = Img;
            this.Refresh();
        }

        private void btnGen_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listOld.Items.Count; i++)
            {
                Bitmap Img = new Bitmap(folderBrowserDialog1.SelectedPath + "//" + listOld.Items[i].ToString());
                Img.RotateFlip(RotateFlipType.Rotate180FlipY);
                Img.Save(folderBrowserDialog1.SelectedPath + "//" + listOld.Items[i].ToString().Split('.')[0] + "_ra.png", ImageFormat.Png);
            }
            MessageBox.Show("操作已完成");
        }

        private void button3_Click(object sender, EventArgs e)
        {

            folderBrowserDialog1.ShowDialog();
            label1.Text = folderBrowserDialog1.SelectedPath;
            path = folderBrowserDialog1.SelectedPath;
            DirectoryInfo root = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
            FileInfo[] pics = root.GetFiles("*.png");

            for (int i = 0; i < pics.Length; i++)
            {
                Image image = System.Drawing.Image.FromFile(folderBrowserDialog1.SelectedPath + "//" + pics[i].Name);
                Bitmap pbitmap = new Bitmap(image);
                Color cro = Color.FromArgb(66, 105, 93);
                pbitmap.MakeTransparent(cro);

                pbitmap.Save(folderBrowserDialog1.SelectedPath + "//new//" + pics[i].Name);
            }

        }

        private void btnGenColor_Click(object sender, EventArgs e)
        {
            if (lstColorList.Items.Count <= 0)
            {
                MessageBox.Show("没有需要更换的颜色");
                return;
            }
            for (int i = 0; i < listOld.Items.Count; i++)
            {
                Bitmap Img = new Bitmap(folderBrowserDialog1.SelectedPath + "//" + listOld.Items[i].ToString());
                //184 ,220,243
                //ChangeColour2(Img, 194, 226, 245, 255, 0, 0);
                //  ChangeColour2(Img, 225, 104, 76, 82, 103, 220);
                //CovertColor(Img, "#FF8266", "#6971FC");

                for (int j = 0; j < lstColorList.Items.Count; j++)
                {
                    string[] colors = lstColorList.Items[j].ToString().Split(',');
                    CovertColor(Img, colors[0], colors[1]);
                }

                if (!Directory.Exists(folderBrowserDialog1.SelectedPath + "//newcolor"))
                    Directory.CreateDirectory(folderBrowserDialog1.SelectedPath + "//newcolor");
                Img.Save(folderBrowserDialog1.SelectedPath + "//newcolor//" + listOld.Items[i].ToString());
            }
            MessageBox.Show("操作已完成");
        }

        public void ChangeColour(Bitmap bmp, byte inColourR, byte inColourG, byte inColourB, byte outColourR, byte outColourG, byte outColourB)
        { // Specify a pixel format.
            PixelFormat pxf = PixelFormat.Format24bppRgb;
            // Lock the bitmap's bits.
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, pxf);
            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;
            // Declare an array to hold the bytes of the bitmap.
            int numBytes = bmp.Width * bmp.Height * 3;
            //int numBytes = bmpData.Stride * bmp.Height; 
            byte[] rgbValues = new byte[numBytes];
            // Copy the RGB values into the array.
            Marshal.Copy(ptr, rgbValues, 0, numBytes); // Manipulate the bitmap
            for (int counter = 0; counter < rgbValues.Length; counter += 3)
            {
                if (rgbValues[counter] == inColourR && rgbValues[counter + 1] == inColourG && rgbValues[counter + 2] == inColourB)
                { rgbValues[counter] = outColourR; rgbValues[counter + 1] = outColourG; rgbValues[counter + 2] = outColourB; }
            } // Copy the RGB values back to the bitmap
            Marshal.Copy(rgbValues, 0, ptr, numBytes); // Unlock the bits.
            bmp.UnlockBits(bmpData);
        }

        public void ChangeColour2(Bitmap bmp, byte inColourR, byte inColourG, byte inColourB, byte outColourR, byte outColourG, byte outColourB)
        {
            // Specify a pixel format.
            PixelFormat pxf = PixelFormat.Format24bppRgb;
            // Lock the bitmap's bits.
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmpData =
            bmp.LockBits(rect, ImageLockMode.ReadWrite,
                         pxf);
            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;
            // Declare an array to hold the bytes of the bitmap. 
            // int numBytes = bmp.Width * bmp.Height * 3; 
            int numBytes = bmpData.Stride * bmp.Height;
            byte[] rgbValues = new byte[numBytes];
            // Copy the RGB values into the array.
            Marshal.Copy(ptr, rgbValues, 0, numBytes);
            // Manipulate the bitmap
            for (int counter = 0; counter < rgbValues.Length; counter += 3)
            {
                if (rgbValues[counter] == inColourR &&
                    rgbValues[counter + 1] == inColourG &&
                    rgbValues[counter + 2] == inColourB)
                {
                    rgbValues[counter] = outColourR;
                    rgbValues[counter + 1] = outColourG;
                    rgbValues[counter + 2] = outColourB;
                }
            }
            // Copy the RGB values back to the bitmap
            Marshal.Copy(rgbValues, 0, ptr, numBytes);
            // Unlock the bits.
            bmp.UnlockBits(bmpData);
        }

        public static Bitmap CovertColor(Bitmap imgObj, string oldColor, string newColor)
        {
            //转换颜色类型 如把#ffffff转换成Color对象
            var Color1 = ColorTranslator.FromHtml(oldColor);
            var Color2 = ColorTranslator.FromHtml(newColor);

            for (var x = 0; x < imgObj.Width; x++)
            {
                for (var j = 0; j < imgObj.Height; j++)
                {
                    //获取像素的颜色
                    Color pixelColor = imgObj.GetPixel(x, j);
                    if (Color1.ToArgb() == pixelColor.ToArgb())
                    {
                        //设置像素的颜色
                        imgObj.SetPixel(x, j, Color2);
                    }
                }
            }
            return imgObj;
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
           

        }

        private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Bitmap temp = (Bitmap)pictureBox1.Image;
            Color pixcolor= temp.GetPixel(e.X, e.Y);
            string oldcolor=  ColorTranslator.ToHtml(Color.FromArgb(pixcolor.R, pixcolor.G,pixcolor.B));

            frmColor fc = new frmColor(oldcolor);
            fc.ShowDialog();
            if (frmColor.resalt != "")
            {
                lstColorList.Items.Add(frmColor.resalt);
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {
            lstColorList.Items.Clear();
        }
    }
}
